package com.example.cloud.service.customer.impl;

import com.example.cloud.dao.customer.SatisRecordRepository;
import com.example.cloud.model.customer.SatisRecord;
import com.example.cloud.model.system.User;
import com.example.cloud.service.customer.SatisRecordService;
import com.example.cloud.utils.BusinessUtil;
import com.example.cloud.utils.RequestUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

/**
 * Created by wangtao on 2018/6/6.
 */
@Service
public class SatisRecordServiceImpl implements SatisRecordService {

    private static final Logger logger = Logger.getLogger(SatisRecordServiceImpl.class);

    @PersistenceContext
    EntityManager em;

    @Autowired
    private SatisRecordRepository satisRecordRepository;

    @Override
    public SatisRecord findOne(Long id) {
        return satisRecordRepository.findOne(id);
    }

    @Override
    @Transactional
    public SatisRecord doSave(SatisRecord record) {
        if(record.getId() == null){
            User user = RequestUtil.getLoginUser();
            record.setAccountId(user.getAccountId());
            record.setOrganId(user.getOrganId());
            record.setOrganName(user.getOrganName());
        }
        BusinessUtil.appendRecord(record);
        return satisRecordRepository.save(record);
    }

    @Override
    @Transactional
    public void doDelete(List<Long> ids) {
        satisRecordRepository.deleteByIdIn(ids);
    }
}
